﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace CowboyAndZombies
{
    public class Background
    {
        public Vector2 posicao;
        public Rectangle retangulo;
        public Texture2D textura;
        private int velocidade = 10;

        public void Inicializacao(GameWindow window)
        {
            retangulo.Width = textura.Width;
            retangulo.Height = textura.Height;

            posicao.X = (textura.Width / 2) * -1;
            posicao.Y = (textura.Height / 2) * -1;

            posicao.X = (((textura.Width / 2) - window.ClientBounds.Width / 2) * -1);
            posicao.Y = (((textura.Height / 2) - window.ClientBounds.Height / 2) * -1);
        }

        public void Update(GameTime gameTime, Cowboy cowboy, GameWindow window)
        {
            KeyboardState keyboard = Keyboard.GetState();

            //Canto superior do background.
            if (posicao.Y >= 0)
            {
                cowboy.Update(gameTime, this, window);
            }

            //Canto inferior do background.
            if (posicao.Y <= (textura.Height - window.ClientBounds.Height) * -1)
            {
                cowboy.Update(gameTime, this, window);
            }

            //Canto esquerdo do background.
            if (posicao.X >= 0)
            {
                cowboy.Update(gameTime, this, window);
            }

            //Canto direito do background.
            if (posicao.X <= (textura.Width - window.ClientBounds.Width) * -1)
            {
                cowboy.Update(gameTime, this, window);
            }

            if (keyboard.IsKeyDown(Keys.W))
            {
                if (cowboy.posicao.Y >= (window.ClientBounds.Height / 2))
                {
                    cowboy.Update(gameTime, this, window);
                }
                else if (posicao.Y <= 0)
                {
                    posicao.Y += velocidade;
                }
            }

            if (keyboard.IsKeyDown(Keys.S))
            {
                if (cowboy.posicao.Y <= (window.ClientBounds.Height / 2))
                {
                    cowboy.Update(gameTime, this, window);
                }
                else if (posicao.Y >= (textura.Height - window.ClientBounds.Height) * -1)
                {
                    posicao.Y -= velocidade;
                }
            }

            if (keyboard.IsKeyDown(Keys.A))
            {
                if (cowboy.posicao.X >= (window.ClientBounds.Width / 2))
                {
                    cowboy.Update(gameTime, this, window);
                }
                else if (posicao.X <= 0)
                {
                    posicao.X += velocidade;
                }
            }

            if (keyboard.IsKeyDown(Keys.D))
            {
                if (cowboy.posicao.X <= (window.ClientBounds.Width / 2))
                {
                    cowboy.Update(gameTime, this, window);
                }
                else if (posicao.X >= (textura.Width - window.ClientBounds.Width) * -1)
                {
                    posicao.X -= velocidade;
                }
            }

        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(textura, posicao, retangulo, Color.White);
        }
    }
}
